
// src/kline/overridesa.js

export const getOverridesa = (theme) => {
  const themes = {
    light: {
      up: '#26A69A',
      down: '#EF5350',
      bg: '#ffffff',
      grid: '#F2F3F5',
      cross: '#CCCCCC',
      border: '#E0E0E0',
      text: '#7D8AA3',
      line: '#D9D9D9'
    },
    dark: {
      up: '#4BBD83',
      down: '#D62548',
      bg: '#0e1327',
      grid: '#35393E',
      cross: '#999',
      border: '#35393E',
      text: '#999',
      line: '#35393E'
    }
  }

  const t = themes[theme]

  return {
    volumePaneSize: 'medium',

    // 展开按钮
    'paneProperties.legendProperties.showLegend': true, // 是否显示展开按钮
    // 标题旁百分比
    'paneProperties.legendProperties.showBarChange': false, // 是否显示标题旁的百分比变化
    'paneProperties.legendProperties.showOnlyPriceSource': false,

    'mainSeriesProperties.priceAxisProperties.autoScale': true, // 是否自动调整价格轴的刻度

    'paneProperties.legendProperties.showSeriesTitle': false,
    'paneProperties.legendProperties.showSeriesOHLC': false,
    // 'paneProperties.legendProperties.showLegend': false,
    // 'paneProperties.legendProperties.showBarChange': false,
    // 'paneProperties.legendProperties.showOnlyPriceSource': false,

    'mainSeriesProperties.style': 3, // 面积图
    'mainSeriesProperties.priceLineWidth': 3,
    'mainSeriesProperties.priceLineColor': '#EF5350',
    'mainSeriesProperties.priceLineStyle': 1,
    'mainSeriesProperties.priceLineVisible': true,
    'scalesProperties.seriesLastValueMode': 1,

    'mainSeriesProperties.lastValueVisible': true,
    'mainSeriesProperties.lastPriceAnimation': 1,
    'mainSeriesProperties.areaStyle.marker': true,
    'mainSeriesProperties.areaStyle.showMarkers': true,
    'mainSeriesProperties.areaStyle.lastValueVisible': true,
    'mainSeriesProperties.areaStyle.lastPriceAnimation': 1,

    // 初始面积图颜色（蓝色）
    'mainSeriesProperties.areaStyle.color1': 'rgba(74,144,226,0.45)',
    'mainSeriesProperties.areaStyle.color2': 'rgba(74,144,226,0.08)',
    'mainSeriesProperties.areaStyle.linecolor': '#4A90E2',
    'mainSeriesProperties.areaStyle.linewidth': 3,
    'mainSeriesProperties.areaStyle.transparency': 70,

    'paneProperties.topMargin': 30,
    'paneProperties.bottomMargin': 5,

    'scalesProperties.lineColor': t.line,
    'scalesProperties.textColor': t.text,
    'scalesProperties.fontSize': 12,
    'paneProperties.background': t.bg,
    'paneProperties.vertGridProperties.color': t.grid,
    'paneProperties.horzGridProperties.color': t.grid,
    'paneProperties.crossHairProperties.color': t.cross,
    'paneProperties.crossHairProperties.style': 2,

    'volume.volume.color.0': t.down,
    'volume.volume.color.1': t.up,
    'volume.volume.transparency': 0,
  }
}

/**
 * 初始化 TradingView widget 后的自定义逻辑
 * @param {Object} widget - TradingView.widget 实例
 * @param {String} theme - 当前主题（light / dark）
 */
export const initChartExtras = (widget, theme = 'light') => {
  const t = {
    light: { up: '#26A69A', down: '#EF5350' },
    dark: { up: '#4BBD83', down: '#D62548' }
  }[theme]

  widget.onChartReady(() => {
    const chart = widget.activeChart()

    // ✅ 默认加载 MA5/10/30，显示在图表上
    const maConfigs = [
      { length: 5, color: '#E5BF39', width: 3 },
      { length: 10, color: '#7CC7BA', width: 2 },
      { length: 30, color: '#C5A0E0', width: 2 }
    ]
    maConfigs.forEach(ma =>
      chart.createStudy('Moving Average', true, false, [ma.length], null, {
        'Plot.color': ma.color,
        'Plot.linewidth': ma.width
      })
    )

    // ✅ 模拟主线发光：创建叠加半透明宽线
    chart.createStudy('Moving Average', true, false, [1], null, {
      'Plot.color': 'rgba(74,144,226,0.2)',
      'Plot.linewidth': 8, // 比主线宽，产生光晕效果
      'Plot.transparency': 50
    })

    // 获取主线 series
    const mainSeries = chart.mainSeries()

    // ✅ 动态更新主线颜色（涨跌）
    mainSeries.subscribeBars((bars) => {
      if (!bars || bars.length < 2) return
      const last = bars[bars.length - 1].close
      const prev = bars[bars.length - 2].close
      const color = last >= prev ? t.up : t.down

      chart.applyOverrides({
        'mainSeriesProperties.priceLineColor': color,
        'mainSeriesProperties.areaStyle.linecolor': color,
        'mainSeriesProperties.areaStyle.color1': `${color}80`,
        'mainSeriesProperties.areaStyle.color2': `${color}20`
      })
    })
  })
}
